﻿using System.Windows;
using System.Windows.Controls;
using System;
using Bridge.Base.Utilities;

namespace Bridge.UI
{
    /// <summary>
    /// Interaction logic for WarningWindow.xaml
    /// </summary>
    public partial class WarningWindow : Window
    {
        /// <summary>
        /// Shows the dialog and waits for a result.
        /// </summary>
        /// <param name="warningType"><see cref="WarningType"/> to pass to the warning.</param>
        /// <param name="warningText">Text to display to the user.</param>
        /// <returns><see cref="WarningResult"/> returned.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="warningText"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="warningText"/> is empty.</exception>
        public static WarningResult ShowDialog(WarningType warningType, string warningText)
        {
            warningText.ThrowIfNullOrEmpty("warningText");

            return ShowDialog(warningType, warningText, string.Empty);
        }

        /// <summary>
        /// Shows the dialog and waits for a result.
        /// </summary>
        /// <param name="warningType"><see cref="WarningType"/> to pass to the warning.</param>
        /// <param name="warningText">Text to display to the user.</param>
        /// <param name="moreInfo">Text to display for extra information.</param>
        /// <returns><see cref="WarningResult"/> returned.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="warningText"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="warningText"/> is empty.</exception>
        public static WarningResult ShowDialog(WarningType warningType, string warningText, string moreInfo)
        {
            warningText.ThrowIfNullOrEmpty("warningText");

            return ShowDialog(warningType, warningText, moreInfo, "Warning");
        }

        /// <summary>
        /// Shows the dialog and waits for a result.
        /// </summary>
        /// <param name="warningType"><see cref="WarningType"/> to pass to the warning.</param>
        /// <param name="warningText">Text to display to the user.</param>
        /// <param name="moreInfo">Text to display for extra information.</param>
        /// <param name="warningTitle">Title of the window to display.</param>
        /// <returns><see cref="WarningResult"/> returned.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="warningText"/> or <paramref name="warningTitle"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="warningText"/> or <paramref name="warningTitle"/> is empty.</exception>
        public static WarningResult ShowDialog(WarningType warningType, string warningText, string moreInfo, string warningTitle)
        {
            warningText.ThrowIfNullOrEmpty("warningText");
            warningTitle.ThrowIfNullOrEmpty("warningTitle");

            WarningWindow warningWindow = new WarningWindow()
            {
                Warning = warningText,
                Title = warningTitle,
                MoreInfo = moreInfo,
                Buttons = warningType
            };
            warningWindow.ShowDialog();

            return warningWindow.Result;
        }

        /// <summary>
        /// Shows the dialog.
        /// </summary>
        /// <param name="warningType"><see cref="WarningType"/> to pass to the warning.</param>
        /// <param name="warningText">Text to display to the user.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="warningText"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="warningText"/> is empty.</exception>
        public static void ShowWarning(WarningType warningType, string warningText)
        {
            warningText.ThrowIfNullOrEmpty("warningText");

            ShowWarning(warningType, warningText, string.Empty);
        }

        /// <summary>
        /// Shows the dialog.
        /// </summary>
        /// <param name="warningType"><see cref="WarningType"/> to pass to the warning.</param>
        /// <param name="warningText">Text to display to the user.</param>
        /// <param name="moreInfo">Text to display for extra information.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="warningText"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="warningText"/> is empty.</exception>
        public static void ShowWarning(WarningType warningType, string warningText, string moreInfo)
        {
            warningText.ThrowIfNullOrEmpty("warningText");

            ShowWarning(warningType, warningText, moreInfo, "Warning");
        }

        /// <summary>
        /// Shows the dialog.
        /// </summary>
        /// <param name="warningType"><see cref="WarningType"/> to pass to the warning.</param>
        /// <param name="warningText">Text to display to the user.</param>
        /// <param name="moreInfo">Text to display for extra information.</param>
        /// <param name="warningTitle">Title of the window to display.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="warningText"/> or <paramref name="warningTitle"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="warningText"/> or <paramref name="warningTitle"/> is empty.</exception>
        public static void ShowWarning(WarningType warningType, string warningText, string moreInfo, string warningTitle)
        {
            warningText.ThrowIfNullOrEmpty("warningText");
            warningTitle.ThrowIfNullOrEmpty("warningTitle");

            WarningWindow warningWindow = new WarningWindow()
            {
                Warning = warningText,
                Title = warningTitle,
                MoreInfo = moreInfo,
                Buttons = warningType
            };
            warningWindow.Show();
        }

        public enum WarningType
        {
            YesNo,
            OKCancel,
            OK,
        }

        public enum WarningResult
        {
            Yes,
            No,
            OK,
            Cancel,
            None
        }

        private WarningWindow()
        {
            InitializeComponent();

            Result = WarningResult.None;
        }

        private string Warning
        {
            set { textBlockWarning.Text = value; }
        }

        private string MoreInfo
        {
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    buttonMoreInfo.IsEnabled = true;
                    textBlockMoreInfo.Text = value;
                }
                else
                {
                    buttonMoreInfo.IsEnabled = false;
                    textBlockMoreInfo.Text = string.Empty;
                }
            }
        }

        private WarningType Buttons
        {
            set
            {
                stackPanelButtons.Children.Clear();

                switch (value)
                {
                    case WarningType.OKCancel:
                        stackPanelButtons.Children.Add(CreateButton("Cancel", WarningResult.Cancel));
                        stackPanelButtons.Children.Add(CreateButton("OK", WarningResult.OK));
                        break;
                    case WarningType.OK:
                        stackPanelButtons.Children.Add(CreateButton("OK", WarningResult.OK));
                        break;
                    case WarningType.YesNo:
                        stackPanelButtons.Children.Add(CreateButton("Yes", WarningResult.Yes));
                        stackPanelButtons.Children.Add(CreateButton("No", WarningResult.No));
                        break;
                }
            }
        }

        private WarningResult Result
        {
            get;
            set;
        }

        private Button CreateButton(string text, WarningResult result)
        {
            Button button = new Button()
            {
                Content = text,
                Margin = new Thickness(0),
                Padding = new Thickness(6, 2, 6, 2),
                Tag = result
            };
            button.Click += new RoutedEventHandler(button_Click);
            return button;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Result = (WarningResult)((Button)sender).Tag;
            Close();
        }

        private void buttonMoreInfo_Click(object sender, RoutedEventArgs e)
        {
            textBlockMoreInfo.Visibility = textBlockMoreInfo.IsVisible ? Visibility.Collapsed : Visibility.Visible;
            buttonMoreInfo.Content = textBlockMoreInfo.IsVisible ? "More Info <<" : "More Info >>";
            Height = textBlockMoreInfo.IsVisible ? 385 : 181;
        }
    }
}
